﻿/*
This file is under Microsoft Public License (MS-PL)
Copyright Yohann MARTEL.  All rights reserved.
Codeplex: http://amf.codeplex.com
AMF Website: http://www.ymartel.com/aspnet-mobile-framework/
*/
using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Mobile.WebControls.Field
{
    /// <summary>
    /// CheckboxList webcontrol
    /// </summary>
    public class CheckboxList : DataBoundControl
    {
        #region Properties
        /// <summary>
        /// Gets or sets the text.
        /// </summary>
        /// <value>The text.</value>
        [Category("Functionality")]
        public string Text { get; set; }

        /// <summary>
        /// Gets or sets the data text field. (Use it for databind)
        /// </summary>
        /// <value>The data text field.</value>
        [Category("Data")]
        public string DataTextField { get; set; }

        /// <summary>
        /// Gets or sets the data value field. (Use it for databind)
        /// </summary>
        /// <value>The data value field.</value>
        [Category("Data")]
        public string DataValueField { get; set; }

        /// <summary>
        /// Gets or sets the data name field. (Use it for databind)
        /// </summary>
        /// <value>The data name field.</value>
        [Category("Data")]
        public string DataNameField { get; set; }

        /// <summary>
        /// Gets or sets the theme.
        /// </summary>
        /// <value>The theme.</value>
        [Category("Appearance")]
        public DataTheme Theme { get; set; }

        /// <summary>
        /// Gets or sets the DataType
        /// </summary>
        /// <value>The type of the data.</value>
        [Category("Appearance")]
        public DataType DataType { get; set; }
        #endregion

        #region Methods
        private void AddLegendControl()
        {
            Legend legend = new Legend {Text = Text};
            Controls.Add(legend);
        }

        protected override void PerformDataBinding(System.Collections.IEnumerable datas)
        {
            AddLegendControl();
            if (datas != null)
            {
                foreach (object dataItem in datas)
                {
                    object propTextField = DataBinder.GetPropertyValue(dataItem, DataTextField);
                    object propValueField = DataBinder.GetPropertyValue(dataItem, DataValueField);
                    object propNameField = DataBinder.GetPropertyValue(dataItem, DataNameField);
                    InputItem inputItem = new InputItem();
                    inputItem.InputType = InputType.Checkbox;
                    if (propTextField != null)
                    {
                        inputItem.LabelText = propTextField.ToString();
                    }
                    if (propValueField != null)
                    {
                        inputItem.FieldValue = propValueField.ToString();
                    }
                    if (propNameField != null)
                    {
                        inputItem.FieldName = propNameField.ToString();
                    }
                    Controls.Add(inputItem);
                }
            }
        }
        #endregion

        #region Render Methods
        public override void RenderBeginTag(HtmlTextWriter writer)
        {
            AddAttributesToRender(writer);
            writer.RenderBeginTag(HtmlTextWriterTag.Fieldset);
        }

        public override void RenderEndTag(HtmlTextWriter writer)
        {
            writer.RenderEndTag();
        }

        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {
            base.AddAttributesToRender(writer);
            writer.AddAttribute(MetadataAttribute.DataRole, DataRole.ControlGroup);
            if (DataType != DataType.Default)
            {
                writer.AddAttribute(MetadataAttribute.DataType, Enum.GetName(typeof(DataType), DataType).ToLower());
            }
        }
        #endregion

    }
}
